<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis 虚拟内存机制深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .timeline-dot {
            position: relative;
        }
        .timeline-dot::before {
            content: '';
            position: absolute;
            width: 20px;
            height: 20px;
            background: #667eea;
            border-radius: 50%;
            left: -10px;
            top: 50%;
            transform: translateY(-50%);
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        .first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .mermaid {
            background: white;
            border-radius: 12px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        .fade-in {
            animation: fadeIn 0.8s ease-in;
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6 fade-in">
        <div class="max-w-6xl mx-auto text-center">
            <div class="mb-6">
                <i class="fas fa-memory text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6">Redis 虚拟内存机制</h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                深入探索 Redis 曾经的内存管理黑科技，理解其设计理念与演进历程
            </p>
            <div class="mt-10 inline-block bg-white bg-opacity-20 backdrop-blur-sm rounded-full px-6 py-3">
                <span class="text-lg font-medium">
                    <i class="fas fa-exclamation-circle mr-2"></i>
                    已在 Redis 6.0 版本中移除
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-7xl mx-auto px-6 py-16">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover fade-in">
            <div class="prose prose-lg max-w-none">
                <p class="text-gray-700 leading-relaxed">
                    <span class="first-letter">R</span>edis 的虚拟内存机制是一项创新性的内存管理技术，它允许将部分数据页从内存中换出到磁盘上，以便腾出更多的内存空间供其他数据使用。这项技术在处理大规模数据集时，能够在一定程度上缓解内存压力，让 Redis 能够管理超出物理内存限制的数据。
                </p>
            </div>
        </div>

        <!-- Architecture Visualization -->
        <div class="mb-16 fade-in">
            <h2 class="text-3xl font-bold mb-8 text-center text-gradient">系统架构概览</h2>
            <div class="mermaid">
                graph TB
                    A[Redis 主进程] --> B[内存管理器]
                    B --> C[数据页面]
                    B --> D[后台线程]
                    C --> E[热数据页]
                    C --> F[冷数据页]
                    D --> G[换出操作]
                    D --> H[换入操作]
                    F --> G
                    G --> I[磁盘存储]
                    I --> H
                    H --> E
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style I fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Key Components -->
        <section class="mb-16 fade-in">
            <h2 class="text-3xl font-bold mb-8 text-center text-gradient">核心组件解析</h2>
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 rounded-2xl p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-purple-500 rounded-full flex items-center justify-center text-white mr-4">
                            <i class="fas fa-file-alt text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">数据页面</h3>
                    </div>
                    <p class="text-gray-700 leading-relaxed">
                        Redis 将内存中的数据划分为多个页面（page），每个页面的大小由 <code class="bg-purple-200 px-2 py-1 rounded">vm-page-size</code> 配置项指定。这种分页机制使得内存管理更加灵活和高效。
                    </p>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-cyan-50 rounded-2xl p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-blue-500 rounded-full flex items-center justify-center text-white mr-4">
                            <i class="fas fa-cogs text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">后台线程</h3>
                    </div>
                    <p class="text-gray-700 leading-relaxed">
                        Redis 通过专门的后台线程执行虚拟内存的换出和换入操作，确保这些 I/O 密集型操作不会阻塞主线程，从而保证服务的响应性能。
                    </p>
                </div>
            </div>
        </section>

        <!-- Process Timeline -->
        <section class="mb-16 fade-in">
            <h2 class="text-3xl font-bold mb-8 text-center text-gradient">虚拟内存工作流程</h2>
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <div class="space-y-8">
                    <div class="flex items-start timeline-dot pl-8">
                        <div class="flex-1">
                            <h3 class="text-xl font-bold text-gray-800 mb-2">
                                <i class="fas fa-upload text-purple-500 mr-2"></i>换出操作
                            </h3>
                            <p class="text-gray-700 leading-relaxed">
                                当内存使用达到预设阈值时，后台线程会智能识别冷数据页面，将其写入磁盘存储。这个过程是异步的，不会影响正常的读写操作。被换出的页面会在元数据中记录其磁盘位置，以便后续快速定位。
                            </p>
                        </div>
                    </div>
                    
                    <div class="flex items-start timeline-dot pl-8">
                        <div class="flex-1">
                            <h3 class="text-xl font-bold text-gray-800 mb-2">
                                <i class="fas fa-download text-blue-500 mr-2"></i>换入操作
                            </h3>
                            <p class="text-gray-700 leading-relaxed">
                                当应用访问已被换出的数据时，Redis 会触发换入操作，将数据从磁盘重新加载到内存中。这个过程对用户完全透明，应用代码无需关心数据的实际存储位置。
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Configuration Parameters -->
        <section class="mb-16 fade-in">
            <h2 class="text-3xl font-bold mb-8 text-center text-gradient">配置参数详解</h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-indigo-500 to-purple-600 text-white rounded-2xl p-6 card-hover">
                    <div class="text-4xl mb-4 opacity-80">
                        <i class="fas fa-toggle-on"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2">vm-enabled</h3>
                    <p class="opacity-90">控制虚拟内存机制的开关，决定是否启用这项功能</p>
                </div>
                
                <div class="bg-gradient-to-br from-purple-500 to-pink-600 text-white rounded-2xl p-6 card-hover">
                    <div class="text-4xl mb-4 opacity-80">
                        <i class="fas fa-hdd"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2">vm-swap-file</h3>
                    <p class="opacity-90">指定虚拟内存使用的交换文件路径，存储换出的数据页</p>
                </div>
                
                <div class="bg-gradient-to-br from-pink-500 to-red-600 text-white rounded-2xl p-6 card-hover">
                    <div class="text-4xl mb-4 opacity-80">
                        <i class="fas fa-memory"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2">vm-max-memory</h3>
                    <p class="opacity-90">设置 Redis 的最大内存限制，触发虚拟内存换出的阈值</p>
                </div>
            </div>
        </section>

        <!-- Use Cases and Considerations -->
        <section class="mb-16 fade-in">
            <h2 class="text-3xl font-bold mb-8 text-center text-gradient">应用场景与注意事项</h2>
            <div class="bg-white rounded-2xl shadow-xl overflow-hidden">
                <div class="grid md:grid-cols-2">
                    <div class="p-8 border-r border-gray-200">
                        <h3 class="text-2xl font-bold mb-6 text-gray-800">
                            <i class="fas fa-check-circle text-green-500 mr-2"></i>适用场景
                        </h3>
                        <ul class="space-y-4">
                            <li class="flex items-start">
                                <i class="fas fa-database text-purple-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-semibold text-gray-800">大规模数据集</h4>
                                    <p class="text-gray-600">当数据总量超过物理内存容量时，通过虚拟内存机制实现数据的分级存储</p>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-chart-line text-purple-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-semibold text-gray-800">成本优化</h4>
                                    <p class="text-gray-600">在内存成本较高的场景下，通过磁盘存储冷数据来降低整体成本</p>
                                </div>
                            </li>
                        </ul>
                    </div>
                    
                    <div class="p-8">
                        <h3 class="text-2xl font-bold mb-6 text-gray-800">
                            <i class="fas fa-exclamation-triangle text-yellow-500 mr-2"></i>注意事项
                        </h3>
                        <ul class="space-y-4">
                            <li class="flex items-start">
                                <i class="fas fa-tachometer-alt text-red-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-semibold text-gray-800">性能影响</h4>
                                    <p class="text-gray-600">磁盘 I/O 速度